package com.owen.server.impl;

import com.owen.dao.ProductDao;
import com.owen.pojo.Product;
import com.owen.server.ProductServer;
import com.owen.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProductServerImpl implements ProductServer {

    @Autowired
    private ProductDao productDao;

    @Autowired
    private RedisUtil redisUtil;

    @Override
    public List<Product> findAll() {
        List<Product> products = (List<Product>)redisUtil.get("findProduct");
        if(products == null){
            products = productDao.findAll();
            redisUtil.set("findProduct", products);
        }
        return products;
    }

    @Override
    public List<Product> findByProduct(Product product) {
        return productDao.findByProduct(product);
    }

    @Override
    public Product findById(Integer id) {
        return productDao.findById(id);
    }

    @Override
    public Integer deleteById(Integer id) {
        Integer integer = productDao.deleteById(id);
        redisUtil.del("findProduct");
        return integer;
    }

    @Override
    public Integer updateById(Product product) {
        Integer integer = productDao.updateById(product);
        redisUtil.del("findProduct");
        return integer;
    }

    @Override
    public Integer addProduct(Product product) {
        Integer integer = productDao.addProduct(product);
        redisUtil.del("findProduct");
        return integer;
    }

    @Override
    public Integer deleteBatch(Integer[] ids) {
        Integer integer = productDao.deleteBatch(ids);
        redisUtil.del("findProduct");
        return integer;
    }
}